<?php

class Login {
    private static $getInstance;
    private $link;
    private $user;
    private $pwd;


    private function __construct($user_info){
        $this->init($user_info);
    }

    private function __clone(){

    }

    private function init($user_info){
        $this->link = $user_info['link'];
    }

    static function getInstance($user_info){
        if(!self::$getInstance instanceof self){
            return self::$getInstance = new self($user_info);
        }
        return self::$getInstance;
    }


    // 判断用户有没有登录
    function isLogin()
    {
        if (isset($_COOKIE['islogin']) && $_COOKIE['islogin'] == 1) {
            echo "您已经登陆！<script>window.location='index.php'</script>";
            exit;
        }
    }

    // 获取cookies
    private function getCookies($arr, $dir = '/')
    {
        foreach ($arr as $key => $val) {
            setcookie($key, $val, time() + 60 * 60, $dir);
        }
    }

    // 删除cookies
    private function delCook($arr, $dir = '/')
    {
        foreach ($arr as $key => $val) {
            setcookie($key, '', time() - 1, $dir);
        }
    }

    //删除session
    private function delSession()
    {
        // 删除所有 Session 变量
        $_SESSION = array();

        //判断 cookie 中是否保存 Session ID
        if (isset($_COOKIE[session_name()])) {
            setcookie(session_name(), '', time() - 3600, '/');
        }
        //彻底销毁 Session
        session_destroy();
    }

    // 登陆函数
    function login($login)
    {
        // if(!empty($_POST[$login])){
        //     $cook = [];
        //     $username = $_POST['username'];
        //     $pwd = md5($_POST['password']);
        //     $cond = "WHERE admin_name = '{$username}' AND admin_pwd = '{$pwd}'";
        //     $admin = select_one('nnd_admin', 'admin_name, admin_pwd', $cond);
        //     if($admin['admin_name'] == $username && $admin['admin_pwd'] == $pwd){
        //         $cook['islogin'] = 1;
        //         $cook['username'] = $username;
        //         get_cookies($cook);
        //         echo "<script>window.location.href='index.php';</script>";
        //     }else{
        //         $cook['islogin'] = 0;
        //         echo "<script>alert('用户名或密码错误！');window.location.href='login.php'</script>";
        //     }
        // }

        if (!empty($_POST[$login])) {
            $this->user = $_POST['username'];
            $this->pwd = md5($_POST['password']);
            $cond = "WHERE admin_name = '{$this->user}' AND admin_pwd = '{$this->pwd}'";
            $admin = $this->link->selectOne('admin', 'admin_name, admin_pwd', $cond);
            if ($admin['admin_name'] == $this->user && $admin['admin_pwd'] == $this->pwd) {
                $_SESSION['islogin'] = 1;
                $_SESSION['username'] = $this->user;
            } else {
                $_SESSION['islogin'] = 0;
                echo "<script>alert('用户名或密码错误！');window.location.href='login.php'</script>";
            }
        }
    }

    function logout(){
        $this->delCook($_COOKIE);
        $this->delSession();
    }
}